package com.hoas.quickstart.threads.chapter04.chapter04_1_7;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {

    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean haveValue = false;

    public void set() {
        try {
            lock.lock();
            while (haveValue == true) {
                System.out.println("有可能★★连续");
                condition.await();
            }
            System.out.println("打印★");
            haveValue = true;
            condition.signalAll();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void get() {
        try {
            lock.lock();
            while (haveValue == false) {
                System.out.println("有可能☆☆连续");
                condition.await();
            }
            System.out.println("打印☆");
            haveValue = false;
            condition.signalAll();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
